package com.edmendst.xmall.controller;

import com.edmendst.xmall.dto.ReviewInfo;
import com.edmendst.xmall.dto.ReviewReplyInfo;
import com.edmendst.xmall.service.ReviewService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/review")
public class ReviewController {
    @Autowired
    private ReviewService reviewService;


//    // 添加评论
//    String addReview(ReviewInfo reviewInfo, String userName, List<MultipartFile> reviewImages) throws JsonProcessingException;
    @RequestMapping(value = "/reviewInfo",method = RequestMethod.POST)
    public String addReview(@RequestPart(value = "reviewInfo", required = true)
                                ReviewInfo reviewInfo,
                            @RequestPart(value = "reviewImages", required = true)
                            List<MultipartFile> reviewImages) throws JsonProcessingException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        String userName = authentication.getName();
        return reviewService.addReview(reviewInfo, userName, reviewImages);
    }

//    //    获取评论列表
//    List<ReviewInfo> getReviewList(Integer productId);
    @GetMapping("/{productId}")
    public List<ReviewInfo> getReviewList(@PathVariable Integer productId) {
        return reviewService.getReviewList(productId);
    }

//    //    评论点赞
//    int reviewLike(Integer reviewId, String userName);
    @PutMapping("/like/{reviewId}")
    public int reviewLike(@PathVariable Integer reviewId) {
        return reviewService.reviewLike(reviewId, SecurityContextHolder.getContext().getAuthentication().getName());
    }

//    //    评论回复
//    String reviewReply(ReviewReplyInfo reviewReplyInfo, String userName);
    @RequestMapping(value = "/reply",method = RequestMethod.POST)
    public String reviewReply(@RequestBody ReviewReplyInfo reviewReplyInfo) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userName = authentication.getName();
        return reviewService.reviewReply(reviewReplyInfo, userName);
    }

}
